package com.youaishujuhui.devops.web.dao.manage;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import com.youaishujuhui.devops.web.domain.manage.SysRole;

@Service
public class SysRoleDao {

	@Resource(name = "nutDao")
    private Dao nutDao;
	
	public SysRole insert(SysRole po) {
		return nutDao.insert(po);
	}
	
	public int countByCole(String code) {
		return nutDao.count(SysRole.class, Cnd.where("code", "=", code));
	}
	
	public SysRole fetchResources(SysRole role) {
		return nutDao.fetchLinks(role, "resources");
	}
	
	public int total() {
		Cnd cnd = Cnd.NEW();
		
		return nutDao.count(SysRole.class, cnd);
	}

	public List<SysRole> query(Map<String, Object> param) {
		Cnd cnd = Cnd.NEW();

		if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
			String sort = (String) param.get("sort");
			
			if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
				String order = (String) param.get("order");
				
				if (order.equals("asc")) {
					//orderBy = Cnd.orderBy().asc(sort);
					
					cnd.asc(sort);
				} else {
					//orderBy = Cnd.orderBy().desc(sort);
					
					cnd.desc(sort);
				}
			} else {
				//orderBy = Cnd.orderBy().asc(sort);
				
				cnd.asc(sort);
			}
		}
		
		int offset = (int) param.get("offset");
		int limit = (int) param.get("limit");
		Pager pager = new Pager(offset / limit + 1, limit);
		
		
		return nutDao.query(SysRole.class, cnd, pager);
		
	}

	public SysRole findById(Integer id) {
		return nutDao.fetch(SysRole.class, id);
	}

	public int update(SysRole po) {
		return nutDao.update(po);
	}

	public void clearResources(SysRole role) {
		nutDao.clearLinks(role, "resources");
	}

	public void insertRelation(SysRole role) {
		nutDao.insertRelation(role, "resources");
	}
}
